package com.mixide.ui.service;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import javax.inject.Singleton;

import org.eclipse.e4.core.di.annotations.Creatable;

import com.mixal.Mix;
import com.mixal.hardware.ComparisonIndicator;
import com.mixal.hardware.Memory;
import com.mixal.hardware.OverflowToggle;
import com.mixal.hardware.Register;
import com.mixal.software.Word;
import com.mixal.software.Words;

@Creatable @Singleton
public class MixHolder {

	private Mix mix;
	private ByteArrayOutputStream out;
	
	public MixHolder() {
		this.out = new ByteArrayOutputStream();
		System.setOut(new PrintStream(this.out, true));
		
		this.mix = new Mix();
		this.mix.memory().setInstruction(2999);
		this.mix.loadCodes("C:\\Users\\luocq\\git\\repository\\com.mixide.ui\\src\\code\\prime-500.mix");
		this.mix.memory().set(0, new Word(2));
		int start = 1995;
		for (Word word : Words.ofString("FIRST FIVE HUNDRED PRIMES")) {
			this.mix.memory().set(start++, word);
		}
		this.mix.memory().set(2024, new Word(2035));
		this.mix.memory().set(2049, new Word(2010));
		this.mix.setNext(3000);
		this.mix.start();
	}
	
	public Mix mix() {
		return this.mix;
	}
	
	public ByteArrayOutputStream out() {
		return this.out;
	}
	
	public Memory memory() {
		return this.mix.memory();
	}
	
	public Register rA() {
		return this.mix.rA();
	}
	
	public Register rX() {
		return this.mix.rX();
	}
	
	public Register rI(int i) {
		return this.mix.rI(i);
	}
	
	public Register rI1() {
		return this.rI(1);
	}
	
	public Register rI2() {
		return this.rI(2);
	}
	
	public Register rI3() {
		return this.rI(3);
	}
	
	public Register rI4() {
		return this.rI(4);
	}
	
	public Register rI5() {
		return this.rI(5);
	}
	
	public Register rI6() {
		return this.rI(6);
	}

	public Register rJ() {
		return this.mix.rJ();
	}
	
	public OverflowToggle overflow() {
		return this.mix.overflow();
	}
	
	public ComparisonIndicator ci() {
		return this.mix.ci();
	}
}
